package com.jwell.bigdata.fragment

import android.annotation.SuppressLint
import android.net.Uri
import android.net.http.SslError
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.*
import android.widget.ProgressBar
import com.jwell.bigdata.R

class LZBFragment : Fragment() {
    private val mUrl = "http://datav.aliyuncs.com/share/eed617bb011100080dd56af74a821b64"
    private var mMWebViewClient: WebChromeClient? = null
    private var web_view: WebView? = null
    private var progress_bar: ProgressBar? = null

    companion object {
        fun getInstance(): LZBFragment {
            return LZBFragment()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var view = LayoutInflater.from(context).inflate(R.layout.fragment_lzb, null)
        web_view=view.findViewById(R.id.web_view)
        progress_bar=view.findViewById(R.id.progress_bar)
        initWeb()
        return view
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initWeb() {

        val settings = web_view?.settings
        settings?.javaScriptEnabled = true
        settings?.javaScriptCanOpenWindowsAutomatically = true
        settings?.allowFileAccess = true
        settings?.loadWithOverviewMode = true
        settings?.useWideViewPort = true
        settings?.pluginState = WebSettings.PluginState.ON
        settings?.cacheMode = WebSettings.LOAD_NO_CACHE
        settings?.domStorageEnabled = true
        settings?.setAppCacheEnabled(true)
        settings?.databaseEnabled = true

        // 设置出现缩放工具
        settings?.setSupportZoom(true)
        settings?.builtInZoomControls = true
        //扩大比例的缩放
        settings?.useWideViewPort = true//自适应屏幕
        settings?.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        settings?.loadWithOverviewMode = true

        mMWebViewClient = MWebViewClient()
        web_view?.webChromeClient = mMWebViewClient
        web_view?.webViewClient = object : WebViewClient() {
            override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
                handler.proceed()
            }

            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {

                return true
            }

            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
            }
        }
        web_view?.loadUrl(Uri.parse(mUrl).toString())
    }

    private inner class MWebViewClient : WebChromeClient() {
        override fun onReceivedTitle(view: WebView, title: String) {
            super.onReceivedTitle(view, title)
        }

        override fun onJsAlert(view: WebView, url: String, message: String, result: JsResult): Boolean {
            result.confirm()
            return true
        }

        override fun onProgressChanged(view: WebView, newProgress: Int) {
            if (progress_bar != null) {
                progress_bar?.progress = newProgress
                if (newProgress == 100) {
                    progress_bar?.visibility = View.GONE
                }
            }
            super.onProgressChanged(view, newProgress)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (web_view != null) {
            //webView.reload();
            web_view?.stopLoading()
            //webview销毁
            web_view?.destroy()
            //webview清理内存
            web_view?.clearCache(true)
            //webview清理历史记录
            web_view?.clearHistory()
        }
    }
}